String.prototype.replaceAll = function (FindText, RepText) {
    regExp = new RegExp(FindText, "g");
    return this.replace(regExp, RepText);
}

var he = HE.getEditor('editor', {
    html:'1',
    width: '100%',
    height: '400px',
    autoHeight: true,
    autoFloat: false,
    topOffset: 0,
    uploadPhoto: true,
    uploadPhotoHandler: '/api/file',
    uploadPhotoSize: 2048,
    uploadPhotoType: 'gif,png,jpg,jpeg',
    uploadPhotoSizeError: '不能上传大于2MB的图片',
    uploadPhotoTypeError: '只能上传gif,png,jpg,jpeg格式的图片',
    lang: 'zh-jian',
    skin: 'HandyEditor',
    externalSkin: '',
    item: ['bold', 'italic', 'strike', 'underline', 'fontSize', 'fontName', 'paragraph', 'color', 'backColor',
        'center', 'left', 'right', 'full', 'indent', 'outdent',
        'link', 'code','textBlock',  'image', 'expression', 'subscript', 'superscript',  'horizontal', 'orderedList', 'unorderedList']
});
$(function(){
	
	$('.btn_wl').click(function(){
		$(".wl_list").css({
			"display":"block",
		});
	});

	$(".wl_btn").click(function () {
        $(".wl_list").css({
            "display":"none",
        })
   	});
   	
   	$(".audio_btn").click(function(){
   		$(".audio").css({
            "display":"none",
        })
   	})
   	
   	$(".btn_music").click(function(){
   		$(".audio").css({
            "display":"block",
        })
   	})


    $("#submit").click(function () {
        var content  = he.getHtml().replaceAll("style=\"width: 100%;\"","")
        var str=content.replaceAll(/<img/, "<img style=\"width: 100%;\"");
        $.ajax({
            type: 'put',
            url: '/api/updatearticle',
            dataType:"json",
            data: {
                'id': $("#article_id").val(),
                'title': $("#article_title").val(),
                'articleType': $("#article_type").val(),
                'isOriginal':$("#article_isOriginal").val(),
                'synopsis':$("#article_introduction").val(),
                'content': str,
                'cover': $(".data_img")[0].src,
                'clickRate': $("#clickRate").val()
            },
            success: function (result) {
               if (result.code==200){
                   layer.msg(result.message,{icon: 1})
                   window.setTimeout(function () {
                       window.location.href="/admin/article"
                   },1000)
               } else {
                   layer.msg(result.message,{icon: 5})
               }
            },
            error:function () {
                layer.msg("系统错误！",{icon: 5});
            }
        });
    })


})

$("#add_img").click(function () {
    var form = new  FormData();
    var file = document.getElementById("file_img").files;
    form.append('file',file[0]);
    $.ajax({
        url:"/api/upload/file",
        type:"post",
        dataType:"json",
        data:form,
        processData:false,
        contentType:false,
        success:function(data){
            if (data.code==200){
                $(".wl_list").css({
                    "display":"none",
                });
            } else {
                layer.msg(data.message,{icon: 5});
            }
            $(".data_img").attr("src",data.data)
        },
        error:function () {
            layer.msg("系统错误！",{icon: 5});
        }
    });
})

$("#file_img").change(function (e) {
    var file = document.getElementById("file_img").files;
    var freader = new FileReader();
    freader.readAsDataURL(file[0]);
    freader.onload = function (e) {
        $("#img_1").attr("src",e.target.result)
    }
})


$(document).ready(function () {
    var id = location.search;var theRequest = new Object();

    if (id.indexOf("?") != -1) {
        id = id.substring(4,id.length);
    }else {
        return false
    }
    $.ajax({
        type: 'get',
        url: '/api/article?id='+id,
        dataType:"json",
        success: function (result) {
            var data = result.data;
            $("#article_id").val(data.id);
            $("#article_title").val(data.title);
            $("#article_type").val(data.articleType);
            $("#article_isOriginal").val(data.isOriginal);
            $("#article_introduction").val(data.synopsis);
            $(".data_img").attr("src",data.cover);
            $(".editor").html(data.content);
            $("#clickRate").val(data.clickRate);
        },
        error:function () {
            layer.msg("系统错误！",{icon: 5});
        }
    });
})